<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}发票`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="billInfo"
      :hide-required-asterisk="drawerProps.isView"
    >
      <p></p>
      <el-form-item label="发票备注" prop="billRemark">
        <el-input v-model="billInfo.billRemark" placeholder="请填写发票备注" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button type="primary" v-show="!drawerProps.isView" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { Money } from "@/api/interface";
//const userStore = useUserStore();

interface DrawerProps {
  title: string;
  isView: boolean;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: ""
});
// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

const billInfo = ref<Partial<Money.ResBillList>>({
  // userStatus: 0,
  // userRole: 8,
  // departmentId: userStore.userInfo.departmentId,
  // departmentName: userStore.userInfo.departmentName,
  // userAvatar: imageLinks[Math.floor(Math.random() * imageLinks.length)]
});

const rules = reactive({
  username: [
    { required: true, message: "请填写发票备注" },
    { min: 1, max: 20, message: "发票备注长度为4到20个字符" }
  ]
});

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    // if (!showDepartment.value) {
    //   // 非分站和库房的配送员，部门id等于role,部门名称等于标签值
    //   userInfo.value.departmentId = userInfo.value.userRole;
    //   userInfo.value.departmentName = findLabelByValue(userInfo.value.userRole);
    // }
    if (!valid) return;
    // if (userInfo.value.departmentId == undefined || userInfo.value.departmentName == undefined) {
    //   ElMessage.error({ message: `请选择部门！` });
    //   return;
    // }
    try {
      await drawerProps.value.api!(billInfo.value);
      ElMessage.success({ message: `${drawerProps.value.title}发票成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
